package cn.school.controller.notice;

import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.notice.dao.SchoolNoticeDao;
import cn.school.notice.entity.SchoolNotice;
import cn.school.notice.service.SchoolNoticeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "t_school_notice => 校园通知管理表")
@RequestMapping("/noticeSchool")
public class SchoolNoticeController {

    @Autowired
    private SchoolNoticeService schoolNoticeService;

    @Autowired
    private SchoolNoticeDao schoolNoticeDao;


    @ApiOperation("getNoticeSchool => 查询校园通知管理表")
    @GetMapping("/getNoticeSchool/{id}")
    public CommonResult<SchoolNotice> getNoticeSchoolById(@PathVariable Long id) {
        SchoolNotice schoolNotices = schoolNoticeService.selectById(id);
        return CommonResult.success(schoolNotices);
    }

    @ApiOperation("addNoticeSchool => 新增校园通知管理表")
    @PostMapping("/addNoticeSchool")
    public CommonResult<Integer> addNotice(@RequestBody SchoolNotice schoolNotice) {
        int insert = schoolNoticeService.insert(schoolNotice);
        if (insert > 0) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("deleteNoticeSchool => 删除校园通知管理表")
    @PostMapping("/deleteNoticeSchool")
    public CommonResult<Integer> deleteNotice(@RequestBody SchoolNotice schoolNotice) {
        int num = schoolNoticeService.deleteNotice(schoolNotice);
        if (num > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("updateNoticeSchool => 修改校园通知管理表")
    @PostMapping("/updateNoticeSchool")
    public CommonResult<Integer> updateNotice(@RequestBody SchoolNotice schoolNotice) {
        int num = schoolNoticeService.update(schoolNotice);
        if (num > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("updateNoticeSchool => 批量修改校园通知管理表")
    @PostMapping("/batchUpdateNotice")
    public CommonResult<Integer> batchUpdateNotice(@RequestBody List<SchoolNotice> list) {
        int num = schoolNoticeService.batchUpdateNotice(list);
        if (num > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("findNoticeSchoolPageList => 分页查询校园通知管理表")
    @PostMapping(value = "/findNoticeSchoolPageList")
    public CommonResult<CommonPage<SchoolNotice>> findNoticeSchoolPageList(@RequestBody SchoolNotice schoolNotice) {
        IPage<SchoolNotice> noticePageList = schoolNoticeService.findNoticePageList(schoolNotice);
        return CommonResult.success(CommonPage.restPage(noticePageList));
    }

    @ApiOperation("sendNotice => 发送通知到用户")
    @PostMapping(value = "/sendNotice")
    public CommonResult<CommonPage<SchoolNotice>> sendNotice(@RequestBody SchoolNotice schoolNotice) {
        schoolNoticeService.sendNoticeToUser(schoolNotice);
        return CommonResult.success();
    }

    @ApiOperation("sendNotice => 查询自己的通知列表")
    @PostMapping(value = "/selfSchoolNotice")
    public CommonResult<CommonPage<SchoolNotice>> findSchoolNoticeByUserPageList(@RequestBody SchoolNotice schoolNotice) {
        IPage<SchoolNotice> pageList = schoolNoticeService.findSchoolNoticeByUserPageList(schoolNotice);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("sendNotice => 查询自己的通知列表")
    @PostMapping(value = "/readyNotice")
    public CommonResult<CommonPage<SchoolNotice>> readyNotice(@RequestBody SchoolNotice schoolNotice) {
        int num = schoolNoticeService.readyNotice(schoolNotice);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


}
